﻿Imports System.Data.SqlClient
Public Class frmCatalogoTanquesDetalle
    Dim Tanques As New clsCatalogoTanques
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoTanquesDetalle_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró el catálogo detallado de Tanques")
        Usuario.Ubicacion("Catálogo de Tanques")
    End Sub
    Private Sub frmCatalogoTanquesDetalle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        If BombaNueva = True Then
            txtTanque.Text = ""
        Else
            txtTanque.Text = BombaSelect
        End If


        'Actualizar()
        CargaUbicaciones()
        CargaAlmacenes()
        CargaTanques()

        Bitacora.RegistraActividad("Ingresó al catálogo detallado de Tanques")
        Usuario.Ubicacion("Catálogo detallado de Tanques")
    End Sub
    Sub Actualizar()
        Dim strNombre As String = ""
        If txtTanque.Text <> "" Then
            strNombre = Me.txtTanque.Text
            Tanques.Nombre = strNombre
        Else
            strNombre = ""
            Tanques.Nombre = strNombre
        End If
        Tanques.Buscar()
        Scatter()
    End Sub
    Sub CalcularLitrosDesdeMedida()
        Dim Altura As Double = txtDiametro.Text
        Dim Longitud As Double = txtLongitud.Text
        Dim Diametro As Double = Altura
        Dim Radio As Double = Diametro / 2
        Dim AlfaRadianes As Double = Math.Acos(1 - (Altura / Radio))
        Dim AlfaGrados As Double = AlfaRadianes * 180 / Math.PI

        Dim Volumen As Double = Longitud * (Radio ^ 2) * (AlfaGrados / 57.3 - (Math.Sin(AlfaRadianes) * Math.Cos(AlfaRadianes)))

        'PARA TAPA ELIPTICA
        Dim H As Double = txtHelipsis.Text
        Dim VolumenH As Double = Math.PI * Diametro ^ 2 * H / 6

        Dim VolumenTotal As Double = (VolumenH * 2) + Volumen
        Dim Litros As Double = VolumenTotal * 1000

        txtCapacidad.Text = Litros
    End Sub
    Private Sub Scatter()
        txtLongitud.Text = Tanques.Longitud
        txtDiametro.Text = Tanques.Diametro
        txtHelipsis.Text = Tanques.AltoGlobo
        txtCapacidad.Text = Tanques.Capacidad
        txtAlmacen.Text = BuscarStrDatoCitra("MGW10003", "cNombreA01", "cidalmacen=" & Tanques.IdAlmacen)
        txtUbicacion.Text = Tanques.Ubicacion

        If Tanques.PosicionTanque = 0 Then
            radioHorizontal.Checked = True
        Else
            radioVertical.Checked = True
        End If

        txtCuentaLitros.Text = Tanques.Cuentalitros
        txtMedidaInicial.Text = Tanques.Medida_Actual


        If Tanques.IdTanque > 0 Then
            lblCuentaLitros.Visible = False
            lblMedidaInicial.Visible = False
            txtCuentaLitros.Visible = False
            txtMedidaInicial.Visible = False
        Else
            lblCuentaLitros.Visible = True
            lblMedidaInicial.Visible = True
            txtCuentaLitros.Visible = True
            txtMedidaInicial.Visible = True
        End If

    End Sub
    Sub CargaAlmacenes()

        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = CitraConnection

            strSql = "Select CNOMBREA01 FROM MGW10003 ORDER BY CNOMBREA01"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtAlmacen.AutoCompleteCustomSource.Add(rdBuscar("CNOMBREA01").ToString.Trim)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

    End Sub
    Sub CargaUbicaciones()

        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = InitialConnection

            strSql = "Select DESCRIPCION FROM UBICACIONES ORDER BY DESCRIPCION"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtUbicacion.AutoCompleteCustomSource.Add(rdBuscar("DESCRIPCION").ToString.Trim)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

    End Sub
    Sub CargaTanques()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarTanques.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarTanques.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from Tanques WHERE UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarTanques.Rows.Add(rdBuscar("Nombre").ToString.Trim)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Private Sub txtBuscarRuta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarTanques.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarTanque()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            panelTanques.Visible = False
            txtAlmacen.SelectAll()
            txtAlmacen.Focus()
        End If
    End Sub
    Private Sub txtBuscarRuta_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarTanques.TextChanged
        CargaTanques()

    End Sub
    Private Sub dgBuscarRuta_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarTanques.DoubleClick
        SeleccionarTanque()
    End Sub

    Private Sub dgBuscarRuta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarTanques.KeyPress
        SeleccionarTanque()
    End Sub
   
    Sub SeleccionarTanque()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarTanques.CurrentCell.ColumnIndex
        valorRow = dgBuscarTanques.CurrentRow

        Dim TanqueMostrar As String = dgBuscarTanques.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtTanque.Text = TanqueMostrar

        panelTanques.Visible = False
        txtTanque.SelectAll()
        txtTanque.Focus()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        panelTanques.Visible = True
        txtBuscarTanques.Clear()
        txtBuscarTanques.Focus()
    End Sub
    Private Sub Gatter()
        Dim IdTanque As Integer = BuscarIntDatoCitra("Tanques", "IdTanque", "Nombre='" & txtTanque.Text.Trim & "'")
        If IdTanque > 0 Then
            Tanques.IdTanque = IdTanque
        Else
            Tanques.IdTanque = (BuscarIntDatoCitra("Select Max(IdTanque) as IdTanque FROM Tanques")) + 1
        End If
        Tanques.Nombre = txtTanque.Text.Trim
        Tanques.Ubicacion = txtUbicacion.Text
        Tanques.Longitud = txtLongitud.Text
        Tanques.Diametro = txtDiametro.Text
        Tanques.AltoGlobo = txtHelipsis.Text
        Tanques.Capacidad = txtCapacidad.Text
        Tanques.Medida_Actual = txtMedidaInicial.Text
        Tanques.IdAlmacen = BuscarIntDatoCitra("MGW10003", "cIdAlmacen", "cNombreA01='" & txtAlmacen.Text.Trim & "'")
        Tanques.IdBomba = 0

        'CALCULAR LITROS ACTUALES
        If radioHorizontal.Checked = True Then
            Dim Altura As Double = txtMedidaInicial.Text
            Dim Longitud As Double = txtLongitud.Text
            Dim Diametro As Double = txtDiametro.Text
            Dim Radio As Double = Diametro / 2
            Dim AlfaRadianes As Double = Math.Acos(1 - (Altura / Radio))
            Dim AlfaGrados As Double = AlfaRadianes * 180 / Math.PI

            Dim Volumen As Double = Longitud * (Radio ^ 2) * (AlfaGrados / 57.3 - (Math.Sin(AlfaRadianes) * Math.Cos(AlfaRadianes)))

            'PARA TAPA ELIPTICA
            Dim H As Double = txtHelipsis.Text
            Dim VolumenH As Double = Math.PI * Diametro ^ 2 * H / 6

            Dim VolumenTotal As Double = (VolumenH * 2) + Volumen
            Dim Litros As Double = VolumenTotal * 1000
            Tanques.LitrosActuales = Litros
            'txtCapacidad.Text = Litros
        Else
            Dim Altura As Double = txtDiametro.Text
            Dim Longitud As Double = txtLongitud.Text
            Dim Diametro As Double = txtDiametro.Text
            Dim Radio As Double = Diametro / 2
            Dim AlfaRadianes As Double = Math.Acos(1 - (Altura / Radio))
            Dim AlfaGrados As Double = AlfaRadianes * 180 / Math.PI

            Dim Volumen As Double = Longitud * (Radio ^ 2) * (AlfaGrados / 57.3 - (Math.Sin(AlfaRadianes) * Math.Cos(AlfaRadianes)))

            'PARA TAPA ELIPTICA
            Dim H As Double = txtHelipsis.Text
            Dim VolumenH As Double = Math.PI * Diametro ^ 2 * H / 6

            Dim VolumenTotal As Double = (VolumenH * 2) + Volumen
            Dim Litros As Double = VolumenTotal * 1000

            Dim LitrosCentimetro As Double = Litros / Altura
            Dim LitrosExistentes As Double = LitrosCentimetro * txtMedidaInicial.Text

            Tanques.LitrosActuales = LitrosExistentes

        End If
        

        'HASTA AQUI


        'PENDIENTE APLICAR FORMULA
        If radioHorizontal.Checked = True Then
            Tanques.PosicionTanque = 0
        Else
            Tanques.PosicionTanque = 1
        End If
        Tanques.UltimaFecha = Now()
        Tanques.Cuentalitros = txtCuentaLitros.Text
        Tanques.LtsAproximados = Tanques.LitrosActuales
        Tanques.IP_Scanner = "0.0.0.0"

    End Sub
    Sub Guardar()
        Gatter()
        Tanques.Guardar()
        frmCatalogoTanques.CargaTanques()
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()

    End Sub

    Private Sub NuevaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NuevaToolStripMenuItem.Click
        txtTanque.Clear()
        txtTanque.Focus()

    End Sub

    Private Sub GuardarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Guardar()

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Guardar()

    End Sub

    Private Sub txtTanque_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTanque.TextChanged
        Actualizar()

    End Sub

    Private Sub txtLongitud_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtLongitud.TextChanged
        If txtLongitud.Text = "" Then
            txtLongitud.Text = 0
        End If

        If txtDiametro.Text = "" Then
            txtDiametro.Text = 0
        End If

        If txtHelipsis.Text = "" Then
            txtHelipsis.Text = 0
        End If

        CalcularLitrosDesdeMedida()

    End Sub

    Private Sub txtDiametro_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDiametro.TextChanged
        If txtLongitud.Text = "" Then
            txtLongitud.Text = 0
        End If

        If txtDiametro.Text = "" Then
            txtDiametro.Text = 0
        End If

        If txtHelipsis.Text = "" Then
            txtHelipsis.Text = 0
        End If


         CalcularLitrosDesdeMedida()
    End Sub

    Private Sub txtHelipsis_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtHelipsis.TextChanged
        If txtLongitud.Text = "" Then
            txtLongitud.Text = 0
        End If

        If txtDiametro.Text = "" Then
            txtDiametro.Text = 0
        End If

        If txtHelipsis.Text = "" Then
            txtHelipsis.Text = 0
        End If


        CalcularLitrosDesdeMedida()
    End Sub


    Private Sub txtExistentes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    
End Class